package com.gpoint.rdp.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.meeting.orders.service.OrdersService;
import com.gpoint.rdp.meeting.orders.service.OrdersQueryService;
import com.gpoint.rdp.meeting.orders.persistence.entity.OrdersPo;
import com.gpoint.rdp.meeting.orders.persistence.entity.OrderItemsPo;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.List;

/**
 * ORDERS 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-22 17:18:13
 *</pre>
 */
@Controller
@RequestMapping("/rdp/orders/orders/")
public class OrdersController extends GenericController{
	@Resource
	private OrdersService ordersService;
	@Resource
	private OrdersQueryService ordersQueryService;
	
	/**
	 * 【ORDERS】列表(分页条件查询)数据
	 *
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = ordersQueryService.query(queryFilter);
			PageList<OrdersPo> ordersList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<OrdersPo> list = OrdersPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				ordersList = new PageList<OrdersPo>(list, pageResult);
			}
			
			return new PageJson(ordersList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * 编辑【ORDERS】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String orderNum=RequestUtil.getString(request, "orderNum");
		OrdersPo orders=null;
		if(StringUtil.isNotEmpty(orderNum)){
			String data = null;
			data = ordersQueryService.loadCascade(orderNum);
			orders = OrdersPo.fromJsonString(data);
		}

		return getAutoView().addObject("orders", orders).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【ORDERS】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String orderNum=RequestUtil.getString(request, "orderNum");
		OrdersPo orders=null;
		if(StringUtil.isNotEmpty(orderNum)){
			String data = null;
			data = ordersQueryService.loadCascade(orderNum);
			orders = OrdersPo.fromJsonString(data);
		}
		return getAutoView().addObject("orders", orders).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【ORDERS】明细页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String orderNum=RequestUtil.getString(request, "orderNum");
		OrdersPo orders=null;
		if(StringUtil.isNotEmpty(orderNum)){
			String data = null;
			data = ordersQueryService.loadCascade(orderNum);
			orders = OrdersPo.fromJsonString(data);
		}
		return getAutoView().addObject("orders", orders).addObject("returnUrl", preUrl);
	}
	
	/** 
	 * 保存【ORDERS】信息
	 *
	 * @param request
	 * @param response
	 * @param  orders
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String json = RequestUtil.getString(request, "json");
			ordersService.saveCascade(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存ORDERS成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对ORDERS操作失败,"+e.getMessage());
			logger.error("对ORDERS操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【ORDERS】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "orderNum");
			ordersService.deleteByIdsCascade(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除ORDERS成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除ORDERS失败，" + e.getMessage());
			logger.error("删除ORDERS失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	 @RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  return mv;
	  
	 }
}